package com.line.service;

import com.alibaba.fastjson.JSONObject;
import com.line.config.cons.CodeCons;
import com.line.config.cons.RedisCons;
import com.line.dao.ExpressRepository;
import com.line.model.entity.ExpressEntity;
import com.line.model.pojo.ErrorResponse;
import com.line.model.pojo.ExpressNoPojo;
import com.line.model.pojo.ExpressSearchPojo;
import com.line.model.pojo.ExpressSendPojo;
import com.line.tools.util.CopierUtils;
import com.line.tools.zto.ZtoService;
import com.line.tools.zto.pojo.traces.ResDataPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ExpressService {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Autowired
	private ExpressRepository expressRepository;

	@Autowired
	private ZtoService ztoService;

	public ErrorResponse send(ExpressSendPojo pojo) {
		ExpressEntity entity = expressRepository.findByPhoneAndExpressNo(pojo.getPhone(), pojo.getExpressNo());
		if (entity == null) {
			entity = new ExpressEntity();
			CopierUtils.copy(pojo, entity);
		} else {
			entity.setShipInfo(pojo.getShipInfo());
		}
		expressRepository.save(entity);
		return new ErrorResponse(CodeCons.SUCCESS);
	}

	public List<ExpressSearchPojo> search(String phone) {
		List<ExpressSearchPojo> listResult = new ArrayList<>();
		List<ExpressEntity> list = expressRepository.findByPhone(phone);
		for (ExpressEntity entity : list) {
			String redisKey = RedisCons.EXPRESS_NO + entity.getExpressNo();
			ExpressNoPojo noPojo = (ExpressNoPojo) redisTemplate.opsForValue().get(redisKey);
			String result = "";
			if (noPojo == null) { // 缓存不存在
				result = ztoService.traces(entity.getExpressNo());
				noPojo = new ExpressNoPojo();
				noPojo.setLastTime(System.currentTimeMillis());
				noPojo.setResult(result);
				redisTemplate.opsForValue().set(redisKey, noPojo);
			} else { // 缓存存在，则判断时间是否已经超过1个小时
				long costTime = System.currentTimeMillis() - noPojo.getLastTime();
				if (costTime > 60 * 60 * 1000) {
					result = ztoService.traces(entity.getExpressNo());
					noPojo.setLastTime(System.currentTimeMillis());
					noPojo.setResult(result);
					redisTemplate.opsForValue().set(redisKey, noPojo);
				} else {
					result = noPojo.getResult();
				}
			}
			JSONObject object = (JSONObject) JSONObject.parse(result);
			ResDataPojo dataPojo = JSONObject.toJavaObject(object, ResDataPojo.class);
			ExpressSearchPojo searchPojo = new ExpressSearchPojo();
			searchPojo.setOpen(true);
			searchPojo.setExpressName(entity.getExpressName());
			searchPojo.setExpressNo(entity.getExpressNo());
			searchPojo.setShipInfo(entity.getShipInfo());
			searchPojo.setSendTime(entity.getSendTimeS());
			searchPojo.setTraces(dataPojo.getData().get(0).getTraces());
			listResult.add(searchPojo);
		}
		return listResult;
	}
}
